<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Control : paket  * di Buat oleh Diar PHP Generator  *  By Diar */

class ctrpaket extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    function index($xAwal = 0, $xSearch = '') {
//  $this->load->view('test.php');
        $idpegawai = $this->session->userdata('idpegawai');
        if (empty($idpegawai)) {
            redirect(site_url(), '');
        }
        if ($xAwal <= -1) {
            $xAwal = 0;
        } $this->session->set_userdata('awal', $xAwal);
        $this->createformpaket('0', $xAwal);
    }

    function createformpaket($xidx, $xAwal = 0, $xSearch = '') {
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('modelgetmenu');
        $xAddJs = '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/baseurl.js"></script>' .
                '<script language="javascript" type="text/javascript" src="' . base_url() . 'resource/ajax/ajaxpaket.js"></script>';
        echo $this->modelgetmenu->SetViewAdmin($this->setDetailFormpaket($xidx), '', '', $xAddJs, '');
    }

    function setDetailFormpaket($xidx) {
        $this->load->helper('form');
        $xBufResult = '';
        $xBufResult = '<div id="stylized" class="myform"><h3>paket</h3><div class="garis"></div>' . form_open_multipart('ctrpaket/inserttable', array('id' => 'form', 'name' => 'form'));
        $this->load->helper('common');
        $xBufResult .= '<input type="hidden" name="edidx" id="edidx" value="0" />';
        $xBufResult .= setForm('ednamapaket', 'Package Name', form_input(getArrayObj('ednamapaket', '', '300'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('edharga', 'Total ($)', form_input(getArrayObj('edharga', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('eddate_mulai', 'Date Start', form_input(getArrayObj('eddate_mulai', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= setForm('eddate_akhir', 'Date End', form_input(getArrayObj('eddate_akhir', '', '100'))) . '<div class="spacer"></div>';
        //$xBufResult .= setForm('edidpegawai', 'idpegawai', form_input(getArrayObj('edidpegawai', '', '100'))) . '<div class="spacer"></div>';
        $xBufResult .= '<div class="garis"></div>' . form_button('btSimpan', 'simpan', 'onclick="dosimpanpaket();"') . form_button('btNew', 'new', 'onclick="doClearpaket();"') . '<div class="spacer"></div><div id="tabledatapaket">' . $this->getlistpaket(0, '') . '</div><div class="spacer"></div>';
        return $xBufResult;
    }

    function getlistpaket($xAwal, $xSearch) {
        $xLimit = 10;
        $this->load->helper('form');
        $this->load->helper('common');
        $xbufResult = tbaddrow(tbaddcell('idx', '', 'width=10%') .
                tbaddcell('Package Name', '', 'width=40%') .
                tbaddcell('Total ($)', '', 'width=10%') .
                tbaddcell('Date Start', '', 'width=10%') .
                tbaddcell('Date End', '', 'width=10%') .
             //   tbaddcell('idpegawai', '', 'width=10%') .
                tbaddcell('Edit/Delete', 'padding:5px;', 'width:10%;text-align:center;'), '', TRUE);
        $this->load->model('modelpaket');
        $xQuery = $this->modelpaket->getListpaket($xAwal, $xLimit, $xSearch);
        foreach ($xQuery->result() as $row) {
            $xButtonEdit = '<img src="' . base_url() . 'resource/imgbtn/edit.png" alt="Edit Data" onclick = "doeditpaket(\'' . $row->idx . '\');" style="border:none;width:20px"/>';
            $xButtonHapus = '<img src="' . base_url() . 'resource/imgbtn/delete_table.png" alt="Hapus Data" onclick = "dohapuspaket(\'' . $row->idx . '\',\'' . substr($row->namapaket, 0, 20) . '\');" style="border:none;">';
            $xbufResult .= tbaddrow(tbaddcell($row->idx) .
                    tbaddcell($row->namapaket) .
                    tbaddcell($row->harga) .
                    tbaddcell($this->datetomysql($row->date_mulai)) .
                    tbaddcell($this->datetomysql($row->date_akhir)) .
                  //  tbaddcell($row->idpegawai) .
                    tbaddcell($xButtonEdit . '&nbsp/&nbsp' . $xButtonHapus));
        }
        $xInput = form_input(getArrayObj('edSearch', '', '200'));
        $xButtonSearch = '<img src="' . base_url() . 'resource/imgbtn/b_view.png" alt="Search Data" onclick = "dosearchpaket(0);" style="border:none;width:30px;height:30px;" />';
        $xButtonPrev = '<img src="' . base_url() . 'resource/imgbtn/b_prevpage.png" style="border:none;width:20px;" onclick = "dosearchpaket(' . ($xAwal - $xLimit) . ');"/>';
        $xButtonNext = '<img src="' . base_url() . 'resource/imgbtn/b_nextpage.png" style="border:none;width:20px;" onclick = "dosearchpaket(' . ($xAwal + $xLimit) . ');" />';
        $xbufResult .= tbaddrow(tbaddcell($xInput . $xButtonSearch, '', 'width=10% colspan=2') .
                tbaddcell($xButtonPrev . '&nbsp&nbsp' . $xButtonNext, '', 'width=40% colspan =10'), '', TRUE);
        $xbufResult = tablegrid($xbufResult);
        return '<div class="tabledata"  style="width:100%;left:-12px;">' . $xbufResult . '</div>';
    }

    function editrecpaket() {
        $xIdEdit = $_POST['edidx'];
        $this->load->model('modelpaket');
        $row = $this->modelpaket->getDetailpaket($xIdEdit);
        $this->load->helper('json');
        $this->json_data['idx'] = $row->idx;
        $this->json_data['namapaket'] = $row->namapaket;
        $this->json_data['harga'] = $row->harga;
        $this->json_data['date_mulai'] = $this->datetomysql($row->date_mulai);
        $this->json_data['date_akhir'] = $this->datetomysql($row->date_akhir);
        $this->json_data['idpegawai'] = $row->idpegawai;
        echo json_encode($this->json_data);
    }

    function deletetablepaket() {
        $edidx = $_POST['edidx'];
        $this->load->model('modelpaket');
        $this->modelpaket->setDeletepaket($edidx);
        $this->load->helper('json');
        echo json_encode(null);
    }

    function searchpaket() {
        $xAwal = $_POST['xAwal'];
        $xSearch = $_POST['xSearch'];
        $this->load->helper('json');
        if (($xAwal + 0) == -99) {
            $xAwal = $this->session->userdata('awal', $xAwal);
        }
        if ($xAwal + 0 <= -1) {
            $xAwal = 0;
            $this->session->set_userdata('awal', $xAwal);
        } else {
            $this->session->set_userdata('awal', $xAwal);
        }
        $this->json_data['tabledatapaket'] = $this->getlistpaket($xAwal, $xSearch);
        echo json_encode($this->json_data);
    }

    function simpanpaket() {
        $this->load->helper('json');
        if (!empty($_POST['edidx'])) {
            $xidx = $_POST['edidx'];
        } else {
            $xidx = '0';
        }
        $xnamapaket = $_POST['ednamapaket'];
        $xharga = $_POST['edharga'];
        $xdate_mulai = $_POST['eddate_mulai'];
        $xdate_akhir = $_POST['eddate_akhir'];
        $xidpegawai = $_POST['edidpegawai'];
        $this->load->model('modelpaket');
        $idpegawai = $this->session->userdata('idpegawai');
        if (!empty($idpegawai)) {
            if ($xidx != '0') {
                $xStr = $this->modelpaket->setUpdatepaket($xidx, $xnamapaket, $xharga, $this->datetomysql($xdate_mulai), $this->datetomysql($xdate_akhir), $xidpegawai);
            } else {
                $xStr = $this->modelpaket->setInsertpaket($xidx, $xnamapaket, $xharga, $this->datetomysql($xdate_mulai), $this->datetomysql($xdate_akhir), $xidpegawai);
            }
        }
        echo json_encode(null);
    }
function datetomysql($xtgl) {
        if (!empty($xtgl)) {
            $array = explode('-', $xtgl);
            if (!empty($array[2])) {
                return $array[2] . '-' . $array[1] . '-' . $array[0];
            } else {
                return '';
            }
        } else {
            return '';
        }
    }
}

?>